package middleware

import (
	"github.com/gin-gonic/gin"
	"go-malphite/app/common/dto"
	"net/http"
	"strconv"
)

func QueryPage() gin.HandlerFunc {
	return func(c *gin.Context) {
		current, err := strconv.Atoi(c.DefaultPostForm("current", "1"))
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "缺少当前页参数！",
			})
			c.Abort()
			return
		}
		size, err := strconv.Atoi(c.DefaultPostForm("size", "10"))
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "缺少当前页数量参数！",
			})
			c.Abort()
			return
		}
		page := dto.PageReq{
			Current: current,
			Size:    size,
		}
		c.Set("page", page)
		c.Next()
	}
}
